package com.wdd.atcrowdfunding.manager.controller;

import com.wdd.atcrowdfunding.bean.Project;
import com.wdd.atcrowdfunding.potal.service.ProjectService;
import com.wdd.atcrowdfunding.util.AjaxResult;
import com.wdd.atcrowdfunding.util.Page;
import com.wdd.atcrowdfunding.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/auth_project")
public class authProjectController {

    @Autowired
    private ProjectService projectService;

    @RequestMapping("/index")
    public String index(){
        return "objReview/auth_project";
    }

    /**
     * 异步加载页面
     * @param pageno
     * @param pagesize
     * @param queryText
     * @return
     */
    @RequestMapping("/doIndex")
    @ResponseBody
    public AjaxResult doIndex(@RequestParam(value = "pageno",required = false,defaultValue = "1") Integer pageno,
                              @RequestParam(value = "pagesize",required = false,defaultValue = "10")Integer pagesize,
                              String queryText){

        AjaxResult ajaxResult = new AjaxResult();
        try{
            Map<String,Object> paramMap = new HashMap();
            paramMap.put("pageno",pageno);
            paramMap.put("pagesize",pagesize);
            if(StringUtil.isNotEmpty(queryText)){
                if (queryText.contains("%")){
                    queryText = queryText.replaceAll("%","\\\\%");
                }
                if (queryText.contains("_")){
                    queryText = queryText.replaceAll("_","\\\\_");
                }
                paramMap.put("queryText",queryText);
            }
            Page<Project> page = projectService.queryProjectPage(paramMap);
            ajaxResult.setSuccess(true);
            ajaxResult.setPage(page);
        }catch (Exception e){
            e.printStackTrace();
            ajaxResult.setSuccess(false);
            ajaxResult.setMessage("查询数据失败");
        }
        return ajaxResult;
    }


    @RequestMapping("/pass")
    public AjaxResult pass(Integer id){
        AjaxResult ajaxResult = new AjaxResult();
        Project project = new Project();
        project.setId(id);
        project.setStatus("1");
        projectService.updateState(project);

        return ajaxResult;
    }

    @RequestMapping("/dispass")
    public AjaxResult dispass(Integer id){
        AjaxResult ajaxResult = new AjaxResult();
        Project project = new Project();
        project.setId(id);
        project.setStatus("2");
        projectService.updateState(project);

        return ajaxResult;
    }

}
